import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import {
  Row,
  Col,
  Card,
  Form,
  Input,
  Button,
  Menu,
  Modal,
  message,
  Divider,
  Popconfirm,
  Upload,
  Checkbox,
  TreeSelect,
} from 'antd';
import { isArray } from 'lodash';
import TableList from '@/components/TableList';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import Add from './DataLevel/add';
import Edit from './DataLevel/edit';
import { ruleP, nameMaxLength, remarkMaxLength } from '@/utils/rule';
import styles from '../search.less';
const FormItem = Form.Item;
/* eslint react/no-multi-comp:0 */
@connect(
  state => ({
    dataqueryPageList: state.dataLevel.dataqueryPageList,
    loadingqueryPageList: state.loading.effects['dataLevel/queryPageList'],
  }),
  dispatch => ({
    queryPageList: e => dispatch({ type: 'dataLevel/queryPageList', payload: e }),
    dataleveldelete: e => dispatch({ type: 'dataLevel/dataleveldelete', payload: e }),
  })
)
@Form.create()
class dataLevel extends PureComponent {
  state = {
    expandForm: false,
    formValues: {},
    selectedRows: [],
  };

  columns = [
    {
      title: '级别名称',
      dataIndex: 'name',
      width: 200,
      key: 'name',
    },
    {
      title: '类别',
      width:'50%',
      dataIndex: 'boundNames',
      render: (text) => (
        <Fragment>
         {text&&text.replace( /,/g,"；" )}
        </Fragment>
      ),
      ellipsis:true,
    },
    {
      title: '备注',
      dataIndex: 'remark',
      key: 'remark',
      ellipsis:true,
    },
    // {
    //   title: '保护措施',
    //   dataIndex: 'measures',
    //   key: 'measures',
    // },
    {
      title: '操作',
      width: 100,
      render: (text, record) => (
        <Fragment>
          <Edit pageList={this.pageList} text={record}>
            <a>编辑</a>
          </Edit>
          <Divider type="vertical" />
          <a onClick={() => this.deleteData(text.levelId)}>删除</a>
        </Fragment>
      ),
    },
  ];

  deleteData = id => {
    Modal.confirm({
      title: '删除',
      content: '您确定要删除以下数据吗？',
      okText: '确认',
      cancelText: '取消',
      onOk: () => this.handleDeleteData(id),
    });
  };

  // 删除方法
  handleDeleteData(id) {
    const values = {
      levelIds: isArray(id) ? id.join(',') : id,
    };
    return this.props.dataleveldelete(values).then(res => {
      if (res.result == 'ok') {
        Modal.success({ title: res.msg });
        this.pageList();
        this.setState({ selectedRows: [] });
      } else {
        Modal.error({ title: '操作失败', content: res.msg });
      }
    });
  }

  // 分页查询列表
  pageList = (data, pageSize = 10, pageNum = 0) => {
    const values = {
      typeName: (data && data.typeName) || '', //类别名称
      pageSize: pageSize, //分页每页条数
      pageNum: pageNum, //分页页码
    };
    this.props.queryPageList(values);
  };

  componentDidMount() {
    this.pageList();
  }

  handleSelectRows = rows => {
    this.setState({
      selectedRows: rows.map(item => item.levelId),
    });
  };

  handleSearch = e => {
    e.preventDefault();

    const { form } = this.props;

    form.validateFields((err, fieldsValue) => {
      if (err) return;
      this.setState({
        formValues: fieldsValue,
      });
      //点击查询
      this.pageList(fieldsValue);
    });
  };

  renderSimpleForm() {
    const {
      form: { getFieldDecorator },
    } = this.props;
    return (
      <Form onSubmit={this.handleSearch} layout="inline">
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
            <FormItem label="类别名称">
              {getFieldDecorator('typeName')(<Input placeholder="请输入" />)}
            </FormItem>
          </Col>

          <Col md={8} sm={24}>
            <span className={styles.submitButtons}>
              <Button type="primary" htmlType="submit">
                查询
              </Button>
            </span>
          </Col>
        </Row>
      </Form>
    );
  }

  renderForm() {
    const { expandForm } = this.state;
    return expandForm ? this.renderSimpleForm() : this.renderSimpleForm();
  }

  handleStandardTableChange(pagination, filters, sorter) {
    const { formValues } = this.state;
    // 分页请求
    this.pageList(formValues, pagination.pageSize, pagination.current - 1);
  }

  render() {
    const { selectedRows } = this.state;
    return (
      <PageHeaderWrapper title="数据分级管理">
        <Card bordered={false}>
          <div className={styles.tableList}>
            {/* <div className={styles.tableListForm}>{this.renderForm()}</div> */}
            <div className={styles.tableListOperator}>
              <Add pageList={this.pageList}>
                <Button icon="plus" type="primary">
                  新增
                </Button>
              </Add>
              {selectedRows.length > 0 && (
                <span>
                  <Button onClick={() => this.deleteData(selectedRows)}>批量删除</Button>
                </span>
              )}
            </div>
            <TableList
              className={styles.tdPadding}
              selectedRows={selectedRows}
              loading={this.props.loadingqueryPageList}
              data={this.props.dataqueryPageList}
              columns={this.columns}
              onSelectRow={this.handleSelectRows}
              onChange={this.handleStandardTableChange.bind(this)}
              rowKey="levelId"
              hideRowSelection
            />
          </div>
        </Card>
      </PageHeaderWrapper>
    );
  }
}

export default dataLevel;
